package com.guigu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.domain.entity.SubjectCategory;
import com.guigu.exception.GlobalException;
import com.guigu.mapper.SubjectCategoryMapper;
import com.guigu.service.ISubjectCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SubjectCategoryServiceImpl extends ServiceImpl<SubjectCategoryMapper, SubjectCategory> implements ISubjectCategoryService {

    @Resource
    private SubjectCategoryMapper subjectCategoryMapper;

    @Override
    public void deleteSubjectCategory(Integer id) {
        SubjectCategory subjectCategory = this.getById(id);
        if(ObjectUtil.isNull(subjectCategory)){
            throw new GlobalException("分类id不存在，删除失败！");
        }
        subjectCategory.setIsDeleted(1);
        this.subjectCategoryMapper.updateById(subjectCategory);
    }

    @Override
    public List<SubjectCategory> queryCategory(SubjectCategory subjectCategory) {
        LambdaQueryWrapper<SubjectCategory> queryWrapper = new LambdaQueryWrapper<>();
        //分类名不为空则查询
        queryWrapper.like(ObjectUtil.isNotEmpty(subjectCategory.getCategoryName()),SubjectCategory::getCategoryName,subjectCategory.getCategoryName());
        //分类类型
        queryWrapper.eq(ObjectUtil.isNotNull(subjectCategory.getCategoryType()),SubjectCategory::getCategoryType,subjectCategory.getCategoryType());
        //父级ID
        queryWrapper.eq(ObjectUtil.isNotNull(subjectCategory.getParentId()),SubjectCategory::getParentId,subjectCategory.getParentId());
        return this.list(queryWrapper);
    }
}
